package com.fhzn.boss.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fhzn.boss.entity.Label;
import com.fhzn.boss.entity.WebResponse;
import com.fhzn.boss.service.LabelService;
import com.fhzn.boss.tools.PageInfoUtils;
import com.fhzn.boss.web.converter.LabelConverter;
import com.fhzn.boss.web.request.LabelQueryRequest;
import com.fhzn.boss.web.request.LabelsOpRequest;
import com.fhzn.boss.web.vo.LabelVO;
import com.fhzn.commons.toolkit.entity.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 老顾
 * @apiNote
 * @since 2024/3/25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/labels")
public class LabelController {

    private final LabelService labelService;

    @GetMapping(value = "appCode/list")
    public WebResponse<List<LabelVO>> labelSetBasedOnAppcode(String appCode) {
        List<Label> labelTypeList = labelService.labelSetBasedOnAppcode(appCode);
        return WebResponse.success(labelTypeList.stream().map(LabelConverter::fromLabel).collect(Collectors.toList()));
    }

    @GetMapping(value = "page")
    public WebResponse<PageInfo<LabelVO>> labelsWithPage(@ParameterObject LabelQueryRequest request) {
        Page<Label> labelPage = labelService.labelsWithPage(request);
        return WebResponse.success(PageInfoUtils.convert2page(labelPage, LabelConverter::fromLabel));
    }

    @PostMapping(value = "add")
    public WebResponse<Integer> addLabels(@RequestBody LabelsOpRequest request) {
        return WebResponse.success(labelService.addLabels(request));
    }

    @PostMapping(value = "update")
    public WebResponse<Integer> updateLabels(@RequestBody LabelsOpRequest request) {
        return WebResponse.success(labelService.updateLabels(request));
    }

    @PostMapping(value = "delete")
    public WebResponse<Void> deleteLabels(@RequestBody LabelsOpRequest request) {
        labelService.deleteLabels(request.getId());
        return WebResponse.success();
    }

    @PostMapping(value = "/import")
    public WebResponse<Void> importPlatformDict(@RequestBody String data) throws JsonProcessingException {
        labelService.importLabels(data);
        return WebResponse.success();
    }
}
